
export default async options => {
  let { url, data, header, method = 'GET', showLoading = true, showErrToast = true } = options
  showLoading && wx.showLoading({
    title: '请稍侯...',
    mask: true
  })
  
  let token = wx.getStorageSync('globalData').token
  token = token ? { 'X-Access-Token': token } : {}
  header = { ...header, ...token }
  // console.log('wx.request 调用开始,url=', url);
  // console.log('wx.request 调用开始,method=',method);
  // console.log('wx.request 调用开始,data=', data);
  // console.log('wx.request 调用开始,header=', header);

  return new Promise((resolve, reject) => {
    wx.request({
      url: url,
      method: method,
      data: data,
      header: header,
      success: res => {
        console.log('wx.request 调用成功', url)
        console.log('wx.request 调用开始,data=', data);
        console.log('wx.request 调用成功', res)
        const { statusCode, data } = res
        if (statusCode == 200) {
          if (data.success) {
            resolve(data)
          } else {
            if (data.msg)
              showErrToast && wx.showToast({ title: data.msg, icon: 'none' })
            else
              showErrToast && wx.showToast({ title: '请求数据失败', icon: 'none'})
            reject(data.msg)
          }
        } else {
          showErrToast && wx.showToast({ title: '请求失败', icon: 'none' })
          reject(data.error)
        }
      },
      fail: err => {
        if (err.errMsg == 'request:fail timeout') {
          showErrToast && wx.showToast({ title: '请求超时', icon: 'none' })
        } else {
          reject(err);  
        }
      },
      complete: () => {
        showLoading && wx.hideLoading()
      }
    })
  })

}
